package com.dreamtour.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.OSSConfig;
import com.dreamtour.entity.Servicedesigners;
import com.dreamtour.service.IServicedesignersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/servicedesigners")
public class ServicedesignersController {
    @Autowired
    private IServicedesignersService servicedesignersService;

    @RequestMapping("/getByPage")
    public IPage<Servicedesigners> getByPage(Integer pageNum, Integer pageSize) {
        IPage<Servicedesigners> page = servicedesignersService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @RequestMapping("/getByID")
    public List<Servicedesigners> getByName(String designerId) {
        QueryWrapper<Servicedesigners> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("designerId", designerId);
        List<Servicedesigners> list = servicedesignersService.list(queryWrapper);
        return list;
    }

    @GetMapping("/filter")
    public ResponseEntity<Map<String, Object>> filterServicedesigners(
            @RequestParam(required = false) String qualifyStatus) {

        QueryWrapper<Servicedesigners> queryWrapper = new QueryWrapper<>();

        // 添加验证状态筛选条件
        if (qualifyStatus != null && !qualifyStatus.isEmpty()) {
            // 假设qualifyStatus的值是"0", "1", "2"分别代表，已通过，待审核,未通过
            // 这里需要根据实际的数据库字段类型和值进行调整
            queryWrapper.eq("qualifyStatus", qualifyStatus);
        }

        // 查询符合条件的旅行社
        List<Servicedesigners> filteredServicedesigners = servicedesignersService.list(queryWrapper);

        // 准备返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("servicedesigners", filteredServicedesigners);

        return ResponseEntity.ok(response);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Servicedesigners servicedesigners) {
        boolean b = servicedesignersService.updateById(servicedesigners);
        System.out.println("**********************************");
        return b;
    }

    @RequestMapping("/updateByUserId")
    public boolean updateByUserId(@RequestBody Servicedesigners servicedesigners) {
        QueryWrapper<Servicedesigners> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", servicedesigners.getUserId());
        boolean b = servicedesignersService.update(servicedesigners, queryWrapper);
        return b;
    }

    @RequestMapping("/getServicedesignersCountByMonth")
    public ResponseEntity<List<Map<String, Object>>> getServicedesignersCountByMonth() {
        List<Map<String, Object>> monthlyCounts = servicedesignersService.countServicedesignersByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyCounts);
    }

    //下载PDF
    @Autowired
    private OSSConfig ossConfig;
    @GetMapping("/getQualificationPath")
    public ResponseEntity<String> getQualificationPath(@RequestParam("designerId") Integer designerId) {
        System.out.println("收到请求，designerId: " + designerId);  // 输出接收到的请求参数
        Servicedesigners servicedesigners = servicedesignersService.getById(designerId);
        if (servicedesigners != null && servicedesigners.getDesignerQualification() != null) {
            String filePath = servicedesigners.getDesignerQualification();
            String objectName = filePath.substring(filePath.lastIndexOf("/") + 1);

            // 创建 OSSClient 实例
            OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());

            // 生成预签名 URL（有效期1小时）
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 1小时有效
            URL signedUrl = ossClient.generatePresignedUrl(ossConfig.getBucketName(), objectName, expiration);

            // 关闭 OSS 客户端
            ossClient.shutdown();

            // 返回预签名 URL
            HttpHeaders headers = new HttpHeaders();
            headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.set("Pragma", "no-cache");
            headers.set("Expires", "0");

            System.out.println("生成的签名 URL: " + signedUrl);  // 打印生成的 URL
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(signedUrl.toString());
        } else {
            return ResponseEntity.status(404).body("未找到资质证明文件路径！");
        }
    }



}
